using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// Functions used for pagination processing
/// </summary>
public class Pagination_Process
{
	public Pagination_Process()
	{
		//
		// TODO: Add constructor logic here
		//
	}
 
    // Prepare Script Compatible with SQL SERVER 2005 or above Pagination
    public static string Prepare_SQLSERVER2005_Pagination(string query, string order,int pagenumber, int pagesize)
    {
        // fetch and return only batch of data starting from star posting to lastbound
        int startindex = (pagenumber - 1) * pagesize + 1;
        int lastbound = (startindex - 1) + pagesize;
        return "SELECT TOP " + pagesize + " * FROM (SELECT ROW_NUMBER() OVER (ORDER BY " + order + ") AS RowNumber," + query + ") AS Results WHERE RowNumber >=" + startindex + " AND RowNumber <=" + lastbound;
    }

    // Prepare Script Compatible with MYSQL Pagination
    public static string Prepare_MySQL_Pagination(string query, int pagenumber, int pagesize)
    {
        // fetch and return only batch of data
        int startindex = (pagenumber - 1) * pagesize;
        return query + "  LIMIT " + startindex + "," + pagesize;
    }

    public static void Process_Pagination(DataList _list, Repeater _rept, LinkButton _prev, LinkButton _next, int _size, int _pagenumber, IEnumerable _source)
    {
        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = _source;
        objPds.AllowPaging = true;

        objPds.PageSize = _size;
        objPds.CurrentPageIndex = (_pagenumber - 1);

        if (!objPds.IsFirstPage)
            _prev.Visible = true;
        else
            _prev.Visible = false;
        if (!objPds.IsLastPage)
            _next.Visible = true;
        else
            _next.Visible = false;
        ArrayList arr = Return_Pagination_Link(objPds.PageCount, 11, _pagenumber);
        if (objPds.PageCount > 1)
        {
            _rept.Visible = true;
            _rept.DataSource = arr;
            _rept.DataBind();
        }
        else
        {
            _rept.Visible = false;
        }
        _list.DataSource = objPds;
        _list.DataBind();
    }

    // Main Pagination Logic
    public static ArrayList Return_Pagination_Link(int TotalPages, int Total_Links, int SelectedPage)
    {
        int i;
        ArrayList arr = new ArrayList();
        if (TotalPages < Total_Links)
        {
            for (i = 1; i <= TotalPages; i++)
            {
                arr.Add(i);
            }
        }
        else
        {
            int startindex = SelectedPage;
            int lowerbound = startindex - (int)Math.Floor((double)Total_Links / 2);
            int upperbound = startindex + (int)Math.Floor((double)Total_Links / 2);
            if (lowerbound < 1)
            {
                //calculate the difference and increment the upper bound
                upperbound = upperbound + (1 - lowerbound);
                lowerbound = 1;
            }
            //if upperbound is greater than total page is
            if (upperbound > TotalPages)
            {
                //calculate the difference and decrement the lower bound
                lowerbound = lowerbound - (upperbound - TotalPages);
                upperbound = TotalPages;
            }
            for (i = lowerbound; i <= upperbound; i++)
            {
                arr.Add(i);
            }


        }
        return arr;

    }

}
